using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Scene;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
namespace MicroMsg.Plugin.Sns.Scene
{
	public class SnsPostContext : IContextBase
	{
		private const string TAG = "SnsPostContext";
		private const string JPEG_POSTFIX = ".jpg";
		private const int JPEG_NORMAL_QUALITY = 70;
		private const int JPEG_MIN_QUALITY = 50;
		public const int MAX_RETRYTIMES = 1;
		public const int STATUS_INIT = 0;
		public const int STATUS_READY = 1;
		public const int STATUS_SENDING = 2;
		public const int STATUS_ERROR = 4;
		public const int STATUS_COMPLETE = 5;
		public int mSnsObjectStatus;
		public SnsInfo localSnsObject;
		public SnsTrans mSnsTrans;
		public uint sync2WeiboFlag;
		public string strContent = "";
		public int privacyFlag;
		public BgImgType bgImgType = BgImgType.MMSNS_SETBGIMG_SELFPIC;
		public string strClientID = "";
		public int objectType = 1;
		public bool needToHandle()
		{
			return this.mSnsObjectStatus == 0;
		}
		public bool isRunning()
		{
			if (this.mSnsObjectStatus != 1)
			{
				if (this.mSnsObjectStatus != 2)
				{
					return false;
				}
			}
			return true;
		}
		public bool needToClean()
		{
			return this.mSnsObjectStatus == 5 || this.mSnsObjectStatus == 4;
		}
		public bool initSnsTransContext()
		{
			if (this.mSnsTrans == null)
			{
				this.mSnsTrans = new SnsTrans();
			}
			this.mSnsTrans.strObjectID = this.localSnsObject.strObjectID;
			this.mSnsTrans.strClientID = MD5Core.GetHashString(Util.getNowMilliseconds() + AccountMgr.getCurAccount().strUsrName);
			this.strClientID = this.mSnsTrans.strClientID;
			this.mSnsTrans.nLocalID = this.localSnsObject.nLocalID;
			this.mSnsTrans.nObjectstatus = 0;
			this.mSnsTrans.nSyncFlag = this.sync2WeiboFlag;
			this.mSnsTrans.snsImgTransList = new SnsMediaTransList();
			TimeLineObject timeLine = SnsInfoMgr.getTimeLine(this.localSnsObject);
			if (timeLine != null && timeLine.content != null && timeLine.content.mediaList != null && timeLine.content.mediaList.get_Count() > 0)
			{
				SnsMediaTransList snsMediaTransList = new SnsMediaTransList();
				using (List<TloMedia>.Enumerator enumerator = timeLine.content.mediaList.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						TloMedia current = enumerator.get_Current();
						SnsMediaTrans snsMediaTrans = new SnsMediaTrans();
						snsMediaTrans.nRetryTimes = 0;
						snsMediaTrans.strClientID = MD5Core.GetHashString(snsMediaTrans.GetHashCode() + Util.getNowMilliseconds());
						snsMediaTrans.nStatus = 0;
						snsMediaTrans.strfileName = current.strUrl;
						if (SnsMediaStorage.isHttp(current.strUrl))
						{
							snsMediaTrans.strUrl = current.strUrl;
							snsMediaTrans.nUrlType = current.nUrlType;
						}
						snsMediaTrans.strThumbFileName = current.strThumb;
						snsMediaTransList.list.Add(snsMediaTrans);
					}
				}
				this.mSnsTrans.snsImgTransList = snsMediaTransList;
				StorageMgr.snsTrans.vmethod_0(this.mSnsTrans);
				return true;
			}
			return false;
		}
		public bool setSnsInfoByTransContext()
		{
			return true;
		}
		public SnsMediaTrans tryGetNeedSendSnsTranImg()
		{
			if (this.mSnsTrans != null && this.mSnsTrans.snsImgTransList != null)
			{
				SnsMediaTrans result;
				using (List<SnsMediaTrans>.Enumerator enumerator = this.mSnsTrans.snsImgTransList.list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						SnsMediaTrans current = enumerator.get_Current();
						if (current.nStatus == 0)
						{
							if (!SnsMediaStorage.isHttp(current.strfileName) || !SnsMediaStorage.isHttp(current.strUrl))
							{
								current.nStatus = 1;
								Log.smethod_5("SnsPostContext", "begin send image:" + current.strfileName);
								result = current;
								return result;
							}
							current.nStatus = 5;
						}
						else
						{
							if (current.nStatus == 4 && current.nRetryTimes < 1)
							{
								Log.smethod_5("SnsPostContext", string.Concat(new object[]
								{
									"Retry image:",
									current.strfileName,
									" nRetryTimes = ",
									current.nRetryTimes + 1
								}));
								current.nStatus = 1;
								result = current;
								return result;
							}
						}
					}
					goto IL_107;
				}
				return result;
				IL_107:
				return null;
			}
			return null;
		}
		public int isAllTransSuccess()
		{
			if (this.mSnsTrans != null && this.mSnsTrans.snsImgTransList != null)
			{
				int result;
				using (List<SnsMediaTrans>.Enumerator enumerator = this.mSnsTrans.snsImgTransList.list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						SnsMediaTrans current = enumerator.get_Current();
						if (current.nStatus != 5)
						{
							result = 0;
							return result;
						}
						if (current.nStatus == 5 && !SnsMediaStorage.isHttp(current.strUrl))
						{
							Log.smethod_1("SnsPostContext", "upload sns img get url error");
							result = 1;
							return result;
						}
					}
					return 2;
				}
				return result;
			}
			return 0;
		}
		public bool isTimeLineImgSuccess()
		{
			TimeLineObject timeLine = SnsInfoMgr.getTimeLine(this.localSnsObject);
			bool result;
			using (List<TloMedia>.Enumerator enumerator = timeLine.content.mediaList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					TloMedia current = enumerator.get_Current();
					if (!SnsMediaStorage.isHttp(current.strUrl))
					{
						result = false;
						return result;
					}
				}
				return true;
			}
			return result;
		}
		public bool fixTimelineImgList()
		{
			if (this.mSnsTrans != null && this.mSnsTrans.snsImgTransList != null)
			{
				List<TloMedia> list = new List<TloMedia>();
				using (List<SnsMediaTrans>.Enumerator enumerator = this.mSnsTrans.snsImgTransList.list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						SnsMediaTrans current = enumerator.get_Current();
						if (current != null)
						{
							list.Add(new TloMedia
							{
								strUrl = current.strUrl,
								nUrlType = current.nUrlType,
								strThumb = current.strThumbUrl,
								nThumbType = current.nThumbUrlType,
								nTotalSize = (float)current.nTotalLenth
							});
						}
					}
				}
				TimeLineObject timeLine = SnsInfoMgr.getTimeLine(this.localSnsObject);
				timeLine.content.mediaList = list;
				SnsInfoMgr.setTimeLine(this.localSnsObject, timeLine);
				return true;
			}
			return false;
		}
		public bool isTimeToEndErrorUpload()
		{
			if (this.mSnsTrans != null && this.mSnsTrans.snsImgTransList != null)
			{
				bool result;
				using (List<SnsMediaTrans>.Enumerator enumerator = this.mSnsTrans.snsImgTransList.list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						SnsMediaTrans current = enumerator.get_Current();
						if (current.nStatus == 4 && current.nRetryTimes >= 1)
						{
							result = true;
							return result;
						}
					}
					return false;
				}
				return result;
			}
			return false;
		}
		public void loadSnsPostContext(int nSnsTranLocalId)
		{
			if (nSnsTranLocalId > 0)
			{
				StorageMgr.msgImg.getByMsgTransID(nSnsTranLocalId);
			}
		}
	}
}
